/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package test.game;

/**
 *
 * @author petroff
 */
import android.content.Context;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.graphics.Canvas;
import android.graphics.Color;
import android.view.View;
import android.view.SurfaceView;
import android.view.SurfaceHolder;

public class GameView extends SurfaceView {

	/**Загружаемая картинка*/
	private Bitmap bmp;
	/**Наше поле рисования*/
	private SurfaceHolder holder;
	/**Объект класса GameManager*/
	private GameManager gameLoopThread;
	/** Координата движения по Х=0*/
	private int x = 0;
	/**Скорость изображения = 1*/
	private int xSpeed = 1;

	//конструктор
	public GameView(Context context) {
		super(context);
		gameLoopThread = new GameManager(this);
		holder = getHolder();
		holder.addCallback(new SurfaceHolder.Callback() {

			public void surfaceDestroyed(SurfaceHolder holder) {
			}

			@Override
			public void surfaceCreated(SurfaceHolder holder) {
				gameLoopThread.setRunning(true);
				gameLoopThread.start();
			}

			@Override
			public void surfaceChanged(SurfaceHolder holder, int format, int width, int height) {
			}
		});
		bmp = BitmapFactory.decodeResource(getResources(), R.drawable.ic_launcher);
	}

	//Рисуем нашу картинку на черном фоне
	protected void onDraw(Canvas canvas) {
		    if (x == getWidth() - bmp.getWidth()) 
             {
                    xSpeed = -1;
             }
             if (x == 0) 
             {
                    xSpeed = 1;
             }
             x = x + xSpeed;
             canvas.drawColor(Color.BLACK);
             canvas.drawBitmap(bmp, x , 10, null);
	}
}